package com.wxg.config.rabbitmq;

import com.wxg.domain.RabbitMqConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author void
 * @date 2019/12/25 16:59
 * @desc
 */
@Configuration
@Conditional(value = RabbitCondition.class)
public class RabbitConfig {

    @Autowired
    private RabbitMqProperties properties;

    /**
     * 此处定义Queue的bean只是为了让rabbitMq自动创建队列,只要队列已存在,注释掉也不影响使用
     * @return
     */
    @Bean
    public Queue helloQueue(){
        return new Queue(RabbitMqConstant.QUEUE_HELLO);
    }

    @Bean
    public Queue objQueue(){
        return new Queue(RabbitMqConstant.QUEUE_OBJECT);
    }

    @Bean
    public Queue manyQueue(){
        return new Queue(RabbitMqConstant.QUEUE_MANY);
    }

    //***************************fanout广播模式*************************

    @Bean
    public Queue fanoutAQueue(){
        return new Queue(RabbitMqConstant.QUEUE_FANOUT_A);
    }

    @Bean
    public Queue fanoutBQueue(){
        return new Queue(RabbitMqConstant.QUEUE_FANOUT_B);
    }

    @Bean
    public Queue fanoutCQueue(){
        return new Queue(RabbitMqConstant.QUEUE_FANOUT_C);
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(RabbitMqConstant.EXCHANGE_FANOUT);
    }

    @Bean
    public Binding bindingExchangeA(Queue fanoutAQueue, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutAQueue).to(fanoutExchange);
    }

    @Bean
    public Binding bindingExchangeB(Queue fanoutBQueue, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutBQueue).to(fanoutExchange);
    }

    @Bean
    public Binding bindingExchangeC(Queue fanoutCQueue, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutCQueue).to(fanoutExchange);
    }

    //***************************************************************************

    @Bean
    public Queue messageQueue() {
        return new Queue(RabbitMqConstant.QUEUE_TOPIC_MESSAGE);
    }

    @Bean
    public Queue messagesQueue() {
        return new Queue(RabbitMqConstant.QUEUE_TOPIC_MESSAGES);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitMqConstant.EXCHANGE_TOPIC);
    }

    @Bean
    public Binding bindingExchangeMessage(Queue messageQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(messageQueue).to(topicExchange).with(RabbitMqConstant.QUEUE_TOPIC_MESSAGE);
    }

    @Bean
    public Binding bindingExchangeMessages(Queue messagesQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(messagesQueue).to(topicExchange).with(RabbitMqConstant.QUEUE_TOPIC_ALL);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(properties.getHost(), properties.getPort());
        connectionFactory.setUsername(properties.getUsername());
        connectionFactory.setPassword(properties.getPassword());
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    /**
     * 必须是prototype类型
     * @return
     */
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }
}
